package ltd.dujiabao.activemq.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.Topic;
import java.time.LocalTime;

/**
 * @author : DuJiabao
 * @Project : active-mq
 * @Time : 2021/5/6 11:42
 * @Desc :
 */
@Service
public class ActiveMQProducer {
  @Autowired
  JmsMessagingTemplate jmsMessagingTemplate;

  @Autowired
  JmsTemplate jmsTemplate;

  @Qualifier(value = "destination")
  @Autowired
  Destination destination;

  @Qualifier(value = "clockTopic")
  @Autowired
  Topic clockTopic;

  public void produceMessage(Object obj) {
    jmsMessagingTemplate.convertAndSend(destination, obj);
  }

  /**
   * 一个时钟定时器，发送消息
   */
  @Scheduled(fixedDelay = 1000)
  public void produceMessageScheduled() {
    jmsMessagingTemplate.convertAndSend(clockTopic, LocalTime.now().toString());
    //jmsTemplate.send(clockTopic, (session) -> session.createTextMessage(LocalTime.now().toString()));
  }

}
